package util

import (
	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing/transport/http"
	"github.com/sirupsen/logrus"
	"gitlab-sync/config"
	"os"
)

// 拉取仓库
func PullGit(name string, url string) (isok bool) {
	options := &git.CloneOptions{
		URL:      url,
		Auth:     &http.BasicAuth{Username: config.GetConfig().OriginGitlab.Username, Password: config.GetConfig().OriginGitlab.Password},
		Progress: os.Stdout,
		Mirror:   true,
	}
	_, err := git.PlainClone(config.GetConfig().Gitlab.DownLocalPath+name, false, options)
	if err != nil {
		logrus.Warn("pull err:", err)
		return false
	}
	return true
}

// 推送仓库
func PushGit(name string, description string, remoteUrl string) (isok bool) {
	repo, err := git.PlainOpen(config.GetConfig().Gitlab.DownLocalPath + name)
	if err != nil {
		logrus.Error("open local repository err:", err)
		return false
	}
	err = repo.Push(&git.PushOptions{
		RemoteURL: remoteUrl,
		Auth:      &http.BasicAuth{Username: config.GetConfig().TargetGitlab.Username, Password: config.GetConfig().TargetGitlab.Password},
		Progress:  os.Stdout,
	})
	if err != nil {
		logrus.Warn("push err:", err)
		return false
	}
	return true
}
